package com.rxh.service.impl;

import com.rxh.mapper.core.CoreOrderChangeMapper;
import com.rxh.mapper.core.CoreOrderMapper;
import com.rxh.mapper.finance.FinanceBusinessReconMoneyMapper;
import com.rxh.mapper.finance.FinanceDrawingMapper;
import com.rxh.pojo.finance.FinanceBusinessReconMoney;
import com.rxh.pojo.finance.FinanceDrawing;
import com.rxh.service.BusinessReconMoneyService;
import com.rxh.vo.VoCoreOrder;
import com.rxh.vo.VoCoreOrderChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 
 * @author xuzm
 * @date 20180409
 *
 */
@Service
public class BusinessReconMoneyServiceImpl implements BusinessReconMoneyService 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	FinanceBusinessReconMoneyMapper businessReconMoneyMapper;
	
	@Autowired
    private CoreOrderMapper orderMapper;

	@Autowired
    private CoreOrderChangeMapper orderChangeMapper;
	
	@Autowired
    private FinanceDrawingMapper drawingMapper;

	@Override
	public List<FinanceBusinessReconMoney> queryBusinessReconList(Map<String, Object> map) {
		
		return businessReconMoneyMapper.queryBusinessReconList(map);

	}
	
	@Override
	public Map<String, Object> queryBusinessReconByParam(FinanceBusinessReconMoney businessReconMoney) {
	
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("merId", businessReconMoney.getMerId());
		paramMap.put("reconType", businessReconMoney.getReconType());
		paramMap.put("reconDate", businessReconMoney.getReconDate());
		paramMap.put("sortFieldName","recon_time");
		paramMap.put("sortType","asc");
		Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
		List<FinanceBusinessReconMoney> businessReconMoneyList = businessReconMoneyMapper.queryBusinessReconList(paramMap);
		resultMap.put("businessReconMoneyList", businessReconMoneyList);
		
		return resultMap;
		
	}	

	@Override
	public Map<String, Object> getDownloadReconData(FinanceBusinessReconMoney businessReconMoney){
		  
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//商户余额详细导出sheet创建
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("merId", businessReconMoney.getMerId());
		paramMap.put("reconTime", businessReconMoney.getReconDate());
		paramMap.put("reconType", businessReconMoney.getReconType());
		List<FinanceBusinessReconMoney> businessReconMoneyList = businessReconMoneyMapper.queryBusinessReconList(paramMap);
		resultMap.put("businessReconMoneyList", businessReconMoneyList);
		return resultMap;

	}

	@Override
	public Map<String, Object> getBusinessReportNeededData(FinanceBusinessReconMoney businessReconMoney) {
		
		   VoCoreOrder order = new VoCoreOrder();
	    	SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
	    	Date tradeTime = null;
			try {
				
				tradeTime = dateFmt.parse(businessReconMoney.getReconDate());
				
			} catch (ParseException e1) {
				
				e1.printStackTrace();
				
			}
	    	order.setMerId(businessReconMoney.getMerId());
	    	order.setTradeTime(tradeTime);   
	    	order.setReconType(businessReconMoney.getReconType());
	    	order.setBondTime(tradeTime);
	    	order.setFinanceTime(tradeTime);
    	
    	    //获取商户余额记录
    	    Map<String, Object> resultMap = getDownloadReconData(businessReconMoney);
	    	
	    	List<VoCoreOrder> settlePlusOrderList = orderMapper.getSettlePlusOrderList(order);
	    	
	    	List<VoCoreOrder> noSettlePlusOrderList = orderMapper.getNoSettlePlusOrderList(order);
	    	
	    	List<VoCoreOrder> deductPlusOrderList = orderMapper.getDeductPlusOrderList(order);
	    	
	    	List<VoCoreOrder> bondPlusOrderList = orderMapper.getBondPlusOrderList(order);
	    	
	    	List<VoCoreOrder> noBondPlusOrderList = orderMapper.getNoBondPlusOrderList(order);
	    	
	    	Map<String,Object> paramMap = new HashMap<String,Object>();
	    	paramMap.put("merId", businessReconMoney.getMerId());
	    	paramMap.put("auditTime", businessReconMoney.getReconDate());
	    	paramMap.put("reconType", businessReconMoney.getReconType());
	    	List<VoCoreOrderChange> orderChangeOrderList = orderChangeMapper.queryOrderChangeList(paramMap);
	    	
	    	paramMap.put("merId", businessReconMoney.getMerId());
	    	paramMap.put("reconDate", businessReconMoney.getReconDate());
	    	paramMap.put("reconType", businessReconMoney.getReconType());
	    	List<FinanceDrawing> drawingList = drawingMapper.queryDrawingAmount(paramMap);
	    	
	    	resultMap.put("settlePlusOrderList",settlePlusOrderList);
	    	resultMap.put("noSettlePlusOrderList",noSettlePlusOrderList );
	    	resultMap.put("deductPlusOrderList",deductPlusOrderList);
	    	resultMap.put("bondPlusOrderList",bondPlusOrderList);
	    	resultMap.put("noBondPlusOrderList",noBondPlusOrderList);
	    	resultMap.put("orderChangeOrderList", orderChangeOrderList);
	    	resultMap.put("drawingList",drawingList );
    	
		    return resultMap;
	  }

	@Override
	public Map<String, Object> getReconDateList(FinanceBusinessReconMoney businessReconMoney) {
		
			Map<String, Object> resultMap = new HashMap<String, Object>();
			
			//商户余额详细导出sheet创建
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("merId", businessReconMoney.getMerId());
			paramMap.put("reconType", businessReconMoney.getReconType());
			List<FinanceBusinessReconMoney> businessReconMoneyList = null;
			if(businessReconMoney.getReconType() == 1){
				
				businessReconMoneyList = businessReconMoneyMapper.getDailyReconDateList(paramMap);
				
			}else {
				
				businessReconMoneyList = businessReconMoneyMapper.getMonthReconDateList(paramMap);	
				
				
			}
			resultMap.put("businessReconMoneyList", businessReconMoneyList);
			return resultMap;
		
	}

	@Override
	public Map<String,Object> getRecentTradeMerIds(FinanceBusinessReconMoney businessReconMoney) {
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		List<FinanceBusinessReconMoney> businessReconMoneyList = businessReconMoneyMapper.getRecentTradeMerIds(businessReconMoney);
		resultMap.put("businessReconMoneyList", businessReconMoneyList);
		return resultMap;
		
		
	}



}
